package com.magicborrow.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.adapter.HotwareAdapter;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.beans.StuffListBean;
import com.magicborrow.utils.VolleyTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class NearWareActivity extends BaseActivity implements VolleyTool.HTTPListener{

    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView mRefreshRecyclerView;
    private View mFootView;
    private LinearLayoutManager mLinearLayoutManager;
    private HotwareAdapter mHomeAdapter;
    private ArrayList<StuffBean> mData;
    private int page = 0;
    private int size = 10;
    private boolean lastPage = false;
    private TextView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hot_ware_activity);
        initView();
        initListener();
        initData();
        sendRequest();
    }

    private void initView() {

         title= (TextView) findViewById(R.id.title);
        title.setText("附近好物");
        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        mRefreshRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mLinearLayoutManager = new LinearLayoutManager(this);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRefreshRecyclerView.setLayoutManager(mLinearLayoutManager);
        mSwipeRefreshLayout.setColorSchemeResources(R.color.title_color,
                R.color.title_color, R.color.title_color,
                R.color.title_color);
    }
    private void initListener() {
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 0;
                lastPage = false;
                sendRequest();
            }
        });
        mRefreshRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && mLinearLayoutManager.findLastVisibleItemPosition() + 1 == mHomeAdapter.getItemCount()) {
                    if(lastPage){return;}
                    showFootview(true);
                    page++;
                    sendRequest();
                }
            }
        });
    }

    private void initData() {
        mData = new ArrayList<>();
        mHomeAdapter = new HotwareAdapter(this);
        mHomeAdapter.setData(mData);
        mRefreshRecyclerView.setAdapter(mHomeAdapter);
    }

    private void sendRequest(){
        Map<String,String> params = new HashMap<>();
        params.put("page",page+"");
        params.put("size",size+"");
        VolleyTool.get(Constants.NEAR_WARE_URL,params,this,0,StuffListBean.class);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    private void showFootview(boolean isVis){
        if(mFootView == null){
            mFootView = mRefreshRecyclerView.findViewWithTag("foot");
        }
        TextView tv = (TextView) mFootView.findViewById(R.id.no_more_textView);
        ProgressBar bar = (ProgressBar) mFootView.findViewById(R.id.load_more_progressBar);
        if(isVis){
            bar.setVisibility(View.VISIBLE);
            tv.setText("正在加载...");
        }else{
            bar.setVisibility(View.GONE);
            tv.setText("没有更多了");
        }
    }

    @Override
    public <T> void onResponse(T t, int requestCode) {
        if(requestCode == 0) {
            StuffListBean bean = (StuffListBean) t;
            if (bean.getCode() == 0) {
                if (page == 0) {
                    if (mSwipeRefreshLayout.isShown()) {
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                    mHomeAdapter.getData().clear();
                    mData.addAll(bean.getData().getContent());
                    mHomeAdapter.notifyDataSetChanged();
                    if(bean.getData().getContent().size() < 10){
                        showFootview(false);
                    }
                } else {
                    if (bean.getData().getContent().size() > 0) {
                        int startPosition = mHomeAdapter.getItemCount();
                        mHomeAdapter.getData().addAll(bean.getData().getContent());
                        mHomeAdapter.notifyItemRangeInserted(startPosition, bean.getData().getContent().size());
                    } else {
                        lastPage = true;
                        showFootview(false);
                    }
                }
            } else {
                showShortMsg(bean.getMessage());
            }
        }
    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {
        showShortMsg("当前无法连接到服务器");
    }
}
